﻿using System;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using XiongSJ.Framework.Common;
using ServiceStack.Redis;

namespace XiongSJ.Framework.Web.API.Controllers
{
    /// <summary>
    /// 登录测试接口
    /// </summary>
    public class GetController : ApiController
    {
        /// <summary>
        /// Redis服务器
        /// </summary>
        protected string host = "127.0.0.1";
        /// <summary>
        /// Redis端口
        /// </summary>
        protected int port = 6379;
        /// <summary>
        /// 初始化Redis
        /// </summary>
        protected RedisClient rclient = null;

        /// <summary>
        /// Init
        /// </summary>
        public GetController()
        {
            rclient = new RedisClient(host, port);
        }

        /// <summary>
        /// 返回字符串【Post】
        /// </summary>
        /// <param name="request">请求实体</param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Get(BaseRequest request)
        {
            string ip = CommonHelper.GetHostAddress();//获得客户端IP地址
            rclient.Set(ip, Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));

            var response = new BaseResponse();
            if (request == null)
            {
                response.msg = "传递的对象为空";
                response.status = "300";
                return response;
            }
            try
            {
                if (string.IsNullOrEmpty(request.appid))
                {
                    response.msg = "传递的密钥为空";
                    response.status = "300";
                    return response;
                }
                if (string.IsNullOrEmpty(request.userid))
                {
                    response.msg = "传递的用户名为空";
                    response.status = "300";
                    return response;
                }
                if (string.IsNullOrEmpty(request.pwd))
                {
                    response.msg = "传递的密码为空";
                    response.status = "300";
                    return response;
                }

                var result = rclient.Get(request.appid + "_" + request.userid + "_" + request.pwd);
                if (result == null)
                {
                    response.msg = "用户不存在";
                    response.status = "200";
                    return response;
                }

                response.msg = "恭喜你，登录成功！";
                response.status = "200";
                return response;
            }
            catch (Exception ex)
            {
                response.msg = ex.Message;
                response.status = "500";
                return response;
            }
        }

        /// <summary>
        /// 返回字符串【Get】
        /// </summary>
        /// <param name="appid">密钥</param>
        /// <param name="userid">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public BaseResponse Get(string appid, string userid, string pwd)
        {
            string ip = CommonHelper.GetHostAddress();//获得客户端IP地址
            rclient.Set(ip, Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));

            var response = new BaseResponse();
            try
            {
                if (string.IsNullOrEmpty(appid))
                {
                    response.msg = "传递的密钥为空";
                    response.status = "300";
                    return response;
                }
                if (string.IsNullOrEmpty(userid))
                {
                    response.msg = "传递的用户名为空";
                    response.status = "300";
                    return response;
                }
                if (string.IsNullOrEmpty(pwd))
                {
                    response.msg = "传递的密码为空";
                    response.status = "300";
                    return response;
                }

                var result = rclient.Get(appid + "_" + userid + "_" + pwd);
                if (result == null)
                {
                    response.msg = "用户不存在";
                    response.status = "200";
                    return response;
                }

                response.msg = "恭喜你，登录成功！";
                response.status = "200";
                return response;
            }
            catch (Exception ex)
            {
                response.msg = ex.Message;
                response.status = "500";
                return response;
            }
        }
    }
}
